package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.nacos.api.config.ConfigService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用获取规则抽象
 * @author chengjian 
 * @date 2021/7/7
 */
public abstract class AbstractDynamicProvider<T> implements DynamicRuleProvider<T> {
    @Resource
    private ConfigService configService;

    /**
     * 通过appName获取所有规则
     * @author chengjian
     * @date 2021/7/7 3:27 下午
     * @param appName dataId
     * @param clazz 规则类型
     */
    public List<T> getRulesByDataId(String appName, Class<T> clazz) {
        try {
            return NacosConfigUtil.getRuleEntitiesFromNacos(this.configService, appName, ruleName(), clazz);
        } catch (Exception e){
            e.printStackTrace();
        }

        return new ArrayList<>();
    }

    /**
     * 规则后缀名称
     * @author chengjian
     * @date 2021/7/7 5:14 下午
     * @return String
     */
    public abstract String ruleName();
}
